import { defineStore } from 'pinia'
import { ref } from 'vue'
import type { Contractor, ContractorForm } from '@/types'

export const useContractorStore = defineStore('contractor', () => {
  const contractors = ref<Contractor[]>([
    {
      id: 1,
      name: '中海油服钻井技术有限公司',
      code: 'COSL-DT',
      contactPerson: '张经理',
      contactPhone: '13800138001',
      contactEmail: 'zhang.manager@cosl.com',
      address: '北京市朝阳区建国门外大街1号',
      qualificationLevel: 'A',
      businessScope: ['钻井作业', '完井作业', '修井作业'],
      status: 'active',
      createTime: '2024-01-01 10:00:00',
      updateTime: '2024-01-01 10:00:00'
    },
    {
      id: 2,
      name: '中石化海洋石油工程有限公司',
      code: 'SINOPEC-ME',
      contactPerson: '李工程师',
      contactPhone: '13800138002',
      contactEmail: 'li.engineer@sinopec.com',
      address: '天津市滨海新区海油大道88号',
      qualificationLevel: 'B',
      businessScope: ['海洋工程', '设备维护', '技术支持'],
      status: 'active',
      createTime: '2024-01-01 10:00:00',
      updateTime: '2024-01-01 10:00:00'
    },
    {
      id: 3,
      name: '海油工程装备技术有限公司',
      code: 'COOEC-ET',
      contactPerson: '王主任',
      contactPhone: '13800138003',
      contactEmail: 'wang.director@cooec.com',
      address: '山东省青岛市黄岛区海油路99号',
      qualificationLevel: 'A',
      businessScope: ['装备制造', '技术研发', '设备安装'],
      status: 'active',
      createTime: '2024-01-01 10:00:00',
      updateTime: '2024-01-01 10:00:00'
    },
    {
      id: 4,
      name: '东方地球物理勘探有限责任公司',
      code: 'BGP',
      contactPerson: '刘主管',
      contactPhone: '13800138004',
      contactEmail: 'liu.supervisor@bgp.com',
      address: '河北省涿州市范阳路65号',
      qualificationLevel: 'C',
      businessScope: ['地震勘探', '数据采集', '地质分析'],
      status: 'active',
      createTime: '2024-01-01 10:00:00',
      updateTime: '2024-01-01 10:00:00'
    }
  ])

  // 获取所有承包商
  const getAllContractors = () => {
    return Promise.resolve(contractors.value)
  }

  // 根据ID获取承包商
  const getContractorById = (id: number) => {
    const contractor = contractors.value.find(c => c.id === id)
    return Promise.resolve(contractor)
  }

  // 创建承包商
  const createContractor = (form: ContractorForm) => {
    const newContractor: Contractor = {
      id: Math.max(...contractors.value.map(c => c.id)) + 1,
      ...form,
      createTime: new Date().toLocaleString(),
      updateTime: new Date().toLocaleString()
    }
    contractors.value.push(newContractor)
    return Promise.resolve(newContractor)
  }

  // 更新承包商
  const updateContractor = (id: number, form: ContractorForm) => {
    const index = contractors.value.findIndex(c => c.id === id)
    if (index !== -1) {
      contractors.value[index] = {
        ...contractors.value[index],
        ...form,
        updateTime: new Date().toLocaleString()
      }
      return Promise.resolve(contractors.value[index])
    }
    return Promise.reject(new Error('承包商不存在'))
  }

  // 删除承包商
  const deleteContractor = (id: number) => {
    const index = contractors.value.findIndex(c => c.id === id)
    if (index !== -1) {
      contractors.value.splice(index, 1)
      return Promise.resolve(true)
    }
    return Promise.reject(new Error('承包商不存在'))
  }

  // 根据资质等级获取承包商
  const getContractorsByLevel = (level: 'A' | 'B' | 'C') => {
    const filtered = contractors.value.filter(c => c.qualificationLevel === level)
    return Promise.resolve(filtered)
  }

  // 根据业务范围获取承包商
  const getContractorsByBusinessScope = (scope: string) => {
    const filtered = contractors.value.filter(c => 
      c.businessScope.some(s => s.includes(scope))
    )
    return Promise.resolve(filtered)
  }

  return {
    contractors,
    getAllContractors,
    getContractorById,
    createContractor,
    updateContractor,
    deleteContractor,
    getContractorsByLevel,
    getContractorsByBusinessScope
  }
})
